<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .form_wrap {
      width: 80%;
      margin: 20px auto;
    }

    table {
      width: 80%;
      margin: 20px auto;
    }

    td {
      text-align: center;
    }

    td img {
      width: 100px;
      height: 100px;
    }

    h1 {
      width: 80%;
      text-align: center;
    }

    td {
      white-space: pre;
    }
  </style>
</head>

<body>
  <h1>商品后台管理系统</h1>
  <div class="form_wrap">
    图片：<input type="text" id="imgsrc"> 简介：<input type="text" id="pname">价格：<input id="price" type="text">
    <select id="sel">
      <option value="0">电脑</option>
      <option value="1">冰箱</option>
      <option value="2">手机</option>
    </select>
    <button id="addBtn">添加</button>
    <button id="sureEdit">确认修改</button>
  </div>
  <div>
    <hr>
    <div style="width: 80%; margin: 0 auto;">
      名称：<input type="text" id="searchName">
      价格：<input type="text" id="searchPrice">
      <select id="searchSel">
        <option value="">请选择</option>
        <option value="0">电脑</option>
        <option value="1">冰箱</option>
        <option value="2">手机</option>
      </select>
      <button id="searchBtn">搜索</button>
    </div>
    <table border="1">
      <thead>
        <tr>
          <th>图片</th>
          <th>简介</th>
          <th id="orderPrice">价格</th>
          <th>类别</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="mybody">
        <!-- <tr>
          <td>
            <img src="./images/bx1.jpg" alt="">
          </td>
          <td>海尔冰箱 值得选择</td>
          <td>199</td>
          <td>冰箱</td>
          <td>
            <button>删除</button>
            <button>修改</button>
          </td>
        </tr> -->
      </tbody>
    </table>
  </div>
  <script src="./javascripts/jquery-3.4.1.min.js"></script>
  <script>
    // 调用接口
    $("#addBtn").click(function () {
      if ($("#pname").val() && $("#price").val() && $("#imgsrc").val()) {
        $.ajax({
          url: "http://127.0.0.1:3000/add",
          type: "post",
          data: {
            pname: $("#pname").val(),
            price: $("#price").val(),
            sel: $("#sel").val(),
            img: $("#imgsrc").val()
          },
          success: function (res) {
            console.log(res)
            alert("添加成功");
            show()//添加后重新调用渲染
          }
        })
      } else {
        alert("补全信息")
      }
    })
    // 获取所有数据 或搜索或分类数据  ------------------------------
    function show(price, pname, sel) {
      $.ajax({
        url: "http://127.0.0.1:3000/all",
        type: "post",
        data: {
          price: price,
          pname: pname,
          sel: sel
        },
        success: function (res) {
          xuanRan(res) //调用渲染方法
        }
      })
    }
    show()
    ////删除----------------------------------------------------------------------------
    function del(id) {
      console.log(id)
      $.ajax({
        url: "http://127.0.0.1:3000/del",
        type: "post",
        data: {
          id: id
        },
        success: function (res) {
          console.log(res)
          show()
        }
      })
    }
    // 点击编辑 给各个输入框赋值-------------------------------
    var editId = null;
    function update(id) {
      $.ajax({
        url: "http://127.0.0.1:3000/findData",
        type: "post",
        data: {
          id: id
        },
        success: function (res) {
          // res---------res---------返回当前要修改行的数据
          editId = res._id;//存储修改id
          console.log(res)
          $("#pname").val(res.pname);
          $("#price").val(res.price);
          $("#imgsrc").val(res.img);
          $("#sel").val(res.sel);
        }
      })
    }
    //点击确认修改按钮-----------------------------------------
    $("#sureEdit").click(function () {
      $.ajax({
        url: "http://127.0.0.1:3000/update",
        type: "post",
        data: {
          pname: $("#pname").val(),
          price: $("#price").val(),
          sel: $("#sel").val(),
          img: $("#imgsrc").val(),
          id: editId
        },
        success: function (res) {
          console.log(res);
          show();
        }
      })
    })
    // 搜索功能--------------------------------------------
    $("#searchBtn").click(function () {
      // 点击搜索 传入  价格      名称     分类
      show($("#searchPrice").val(), $("#searchName").val(), $("#searchSel").val())
    })

    // 渲染页面方法
    function xuanRan(res) {
      console.log(res)
      var str = ``
      // 循环生成商品------------
      $.each(res, function (index, ele) {
        var selStr = ''
        if (ele.sel == "0") {
          selStr = "电脑"
        } else if (ele.sel == "1") {
          selStr = "冰箱"
        } else if (ele.sel == "2") {
          selStr = "手机"
        }
        str += `  <tr>
                <td>
                  <img src="./images/${ele.img}.jpg" alt="">
                </td>
                <td >${ele.pname}</td>
                <td>${ele.price}</td>
                <td>${selStr}</td>
                <td>
                  <button onclick="upAndDown('${ele._id}','up')">上移</button>
                  <button onclick="del('${ele._id}')">删除</button>
                  <button onclick="update('${ele._id}')">修改</button>
                  <button onclick="upAndDown('${ele._id}','down')">下移</button>
                </td>
              </tr>`
      })
      // 渲染拼接好的商品
      $("#mybody").html(str)
      // 清空输入框
      $("#pname").val("")
      $("#price").val("")
      $("#imgsrc").val("")
    }

    //1. 安装一个自动重启插件
    //2. npm i nodemon  
    //3. 去 package.json--》scripts ---》start："node ./bin/www" 改成 nodemon ./bin/www
    var num = 0;
    $("#orderPrice").click(function () {
      var orderPrice = null;
      if (num % 3 == 0) {
        orderPrice = 1;
      } else if (num % 3 == 1) {
        orderPrice = -1;
      } else if (num % 3 == 2) {
        orderPrice = "nosort";
      }
      num++;
      $.ajax({
        url: "http://127.0.0.1:3000/orderData",
        type: "post",
        data: {
          orderPrice: orderPrice
        },
        success: function (res) {
          xuanRan(res)  //获取到排序后的数据 调用渲染函数
        }
      })
    })

    // 
    function upAndDown(id, status) {
      $.ajax({
        url: "http://127.0.0.1:3000/upAndDown",
        type: "post",
        data: {
          id: id,
          status: status
        },
        success: function (res) {
          console.log(res)
          xuanRan(res)  //获取到排序后的数据 调用渲染函数
        }
      })
    }
  </script>
</body>

</html>